/**
 * $CD全局变量
 */
function getUtils() {
  return {
    store: window.qiBao?.$store
  }
}

const pageCD = {}
export default {
  // 设置cd
  _set($CD) {
    const { code } = $CD
    pageCD[code] = $CD
    getUtils().store.dispatch('page/setPageInfo', window.$CD.pageInfo)
  },
  // 设置cd-通过code
  _reset(code) {
    if (pageCD[code]) {
      window.$CD = pageCD[code]
      getUtils().store.dispatch('page/setPageInfo', window.$CD.pageInfo)
    }
  },
  // 删除cd
  _unset({ code, pcode = '' }) {
    console.log('_unset 执行了', code, pcode)
    delete pageCD[code]
    if (pcode && pageCD[pcode]) {
      // 弹框才会走这部分逻辑
      window.$CD = pageCD[pcode]
      const treeInfo = JSON.parse(JSON.stringify(window.$CD.treeInfo || {}))
      getUtils().store.dispatch('page/setPageInfo', window.$CD.pageInfo)
      getUtils().store.commit('page/SET_CURRENTTREE', treeInfo)
    }
  }
}
